package nsq_setting

import (
	"GraduationProject/public/db"
	"GraduationProject/public/models"
	"GraduationProject/webserver/controllers/util"
	"github.com/gin-gonic/gin"
)

func NsqSettingsWorker(c *gin.Context) {
	_, err := util.AdminFilter(c)
	if err != nil {
		return
	}

	workerID, err := util.PostFormInt(c, "worker_id")
	if err != nil {
		return
	}

	var count int
	db.DB.Model(&models.NsqSetting{}).
		Where("worker_id = ?", workerID).Count(&count)
	var settings []models.NsqSetting
	db.DB.Where("worker_id = ?", workerID).
		Limit(c.PostForm("limit")).Preload("Seed").
		Offset(c.PostForm("offset")).Order("id desc").Find(&settings)

	c.JSON(200, gin.H{
		"status":   10000,
		"message":  "success",
		"settings": settings,
		"count":    count,
	})
}

func NsqSettingsSeed(c *gin.Context) {
	_, err := util.AdminFilter(c)
	if err != nil {
		return
	}

	workerID, err := util.PostFormInt(c, "seed_id")
	if err != nil {
		return
	}

	var count int
	db.DB.Model(&models.NsqSetting{}).
		Where("seed_id = ?", workerID).Count(&count)
	var settings []models.NsqSetting
	db.DB.Where("seed_id = ?", workerID).
		Limit(c.PostForm("limit")).Preload("Worker").
		Offset(c.PostForm("offset")).Order("id desc").Find(&settings)

	c.JSON(200, gin.H{
		"status":   10000,
		"message":  "success",
		"settings": settings,
		"count":    count,
	})
}
